<template>
  <div id="bodypage">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/homePage' }"><span>主页</span></el-breadcrumb-item>
      <el-breadcrumb-item><span>数据分析</span></el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 图表区 -->
    <div id="wrap">
      <!-- 羊绒信息统计图 -->
      <div id="cashmereFineness">
        <div class="title">羊绒细度统计图（2019年）</div>
        <div class="setOption">
          <input class="el-input__inner" type="number" placeholder="请输入最小值" />
          <input class="el-input__inner" type="number" placeholder="请输入最大值" />
          <input class="el-input__inner" type="number" placeholder="请输入数据条目数" />
          <input type="button" value="确定" @click="submit" />
        </div>
        <div id="cashmereFineness-chart"></div>
      </div>

      <!-- 羊性别年龄饼图 -->
      <div id="sex-Age">
        <div class="title">绒山羊性别年龄比例图（2019年）</div>
        <div id="sex-Age-chart"></div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    methods: {
      // 1. 羊绒细度统计图
      cashmereFineness() {
        var myChart = this.$echarts.init(
          document.getElementById("cashmereFineness-chart")
        );
        // 发送请求获取要渲染的数据
        // this.$axios({
        //   method: "get",
        //   url: "/woolDiameter",
        // }).then((response) => {
        //   this.Fineness = response.data;
        // });

        var option;

        var dataAxis = [
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
          "",
        ];
        //  console.log(dataAxis[0])
        var data = [
          220, 182, 191, 234, 290, 330, 310, 123, 442, 321, 90, 149, 210, 122,
          133, 334, 198, 123, 125, 220,
        ];
        var yMax = 500;
        var dataShadow = [];

        for (var i = 0; i < data.length; i++) {
          dataShadow.push(yMax);
        }

        option = {
          title: {
            text: " ",
            subtext: " ",
          },
          xAxis: {
            data: dataAxis,
            axisLabel: {
              inside: true,
              textStyle: {
                color: "#fff",
              },
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            z: 10,
          },
          yAxis: {
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              textStyle: {
                color: "#999",
              },
            },
          },
          dataZoom: [{
            type: "inside",
          }, ],
          series: [{
            type: "bar",
            showBackground: true,
            itemStyle: {
              color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: "#83bff6",
                },
                {
                  offset: 0.5,
                  color: "#188df0",
                },
                {
                  offset: 1,
                  color: "#188df0",
                },
              ]),
            },
            emphasis: {
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: "#2378f7",
                  },
                  {
                    offset: 0.7,
                    color: "#2378f7",
                  },
                  {
                    offset: 1,
                    color: "#83bff6",
                  },
                ]),
              },
            },
            data: data,
          }, ],
        };

        // Enable data zoom when user click bar.
        var zoomSize = 6;
        myChart.on("click", function(params) {
          // 点击进行查询该条目的所有数据
          console.log(params);
        });

        option && myChart.setOption(option);
      },

      // 2. 羊性别年龄饼图
      sexAge() {
        var chartDom = document.getElementById("sex-Age-chart");
        var myChart = this.$echarts.init(chartDom);
        var option;

        var option;

        option = {
          tooltip: {
            trigger: "item",
          },
          legend: {
            top: "5%",
            left: "center",
          },
          series: [{
            name: "访问来源",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [{
                value: 65,
                name: "成年公羊",
              },
              {
                value: 427,
                name: "成年母羊",
              },
              {
                value: 178,
                name: "周年公羊",
              },
              {
                value: 205,
                name: "周年母羊",
              },
            ],
          }, ],
        };

        option && myChart.setOption(option);
      },

      /**
       * 3、重新渲染统计图（羊绒细度统计图2019）
       */
      submit() {
        // 获取三个输入框
        let inputs = document.querySelectorAll(".setOption>input[type='number']");

        // 获取到三个值
        let min = inputs[0].value;
        let max = inputs[1].value;
        let num = inputs[2].value;
        let range = 0; // 每组数据的长度
        let arr = []; // 统计表横坐标

        // 如果三个值都合法
        if ((min > 0 && max > 0 && num > 0) && num % 1 == 0 && max > min) {

          // 计算每组数据的长度
          range = (max - min) / num;
          // 计算统计表横坐标
          for (let i = 0; i <= num; i++) {
            arr[i] = +min + range * i;
          }
          let reqData =  arr;
          console.log(reqData);

          // 将坐标值传递给后端
          this.$axios({
            method: "post",
            url: "/woolDiameter",
            data:{
              reqData: reqData
            }
          }).then(response => {
            console.log(response.headers);
          });

        } else {
          // 如果不合法，给用户一个提示
          alert("请重新输入");
        }

      },
    },

    mounted() {
      // 初始化统计图
      this.cashmereFineness();
      // 初始化饼图
      this.sexAge();
    },
  };
</script>

<style Lang="less" scoped>
  /* 默认样式 */
  .title {
    text-align: center;
    margin-top: 50px;
    font-size: 40px;
    font-weight: bold;
  }

  /* 羊绒细度图表 */

  /* 图标的选项框 */
  .setOption {
    width: 70%;
    margin: 0 auto;
    display: flex;
    justify-content: space-evenly;
    position: relative;
    top: 60px;
  }

  .setOption input[type="number"] {
    width: 220px;
    height: 40px;
    padding-right: 0px;
  }

  .setOption input[type="button"] {
    color: #fff;
    background-color: transparent;
    position: relative;
    width: 100px;
    height: 40px;
    line-height: 35px;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
    background-image: linear-gradient(to right, #69ff97, #00e4ff, #69ff97);
    border: none;
    border-radius: 30px;
    cursor: pointer;
    background-size: 400%;
    z-index: 1;
    box-shadow: 5px 10px 20px rgba(0, 0, 0, 0.2);
    margin-left: 3%;
  }

  #cashmereFineness {
    width: 100%;
  }

  #cashmereFineness-chart {
    width: 80%;
    height: 600px;
    /* max-width: 90vw; */
    max-height: 60vh;
    margin: 100px auto 0px;
    padding-bottom: 100px;
  }

  #cashmereFineness-chart>canvas {
    width: 100%;
    height: 100%;
    padding-bottom: 100px;
    margin: 0 auto;
  }

  /* 羊性别年龄饼图 */
  #sex-Age {
    width: 100%;
  }

  #sex-Age-chart {
    width: 90%;
    height: 1000px;
    /* max-width: 90vw; */
    max-height: 60vh;
    margin: 100px auto 0px;
    padding-bottom: 100px;
  }

  #sex-Age-chart>canvas {
    width: 100%;
    height: 100%;
    padding-bottom: 100px;
    margin: 0 auto;
  }
</style>
